package com.kailong.servlet.student;

import com.kailong.dao.ManageReserve;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

@WebServlet("/student/doSeatReserve")
public class DoSeatReserve extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        HttpSession session = req.getSession();
        String sno = (String) session.getAttribute("sno");
        String seatTno = req.getParameter("seatTno");
        String tsquare = req.getParameter("tsquare");
        String tfloor = req.getParameter("tfloor");
        String sclass = (String) session.getAttribute("sclass");
        String startTime = req.getParameter("startTime");
        String startDate = req.getParameter("startDate");
        String endDate = req.getParameter("endDate");
        String endTime = req.getParameter("endTime");
        String timeStart = startDate + " " + startTime;
        String timeEnd = endDate + " " + endTime;
        try {
            PrintWriter out = resp.getWriter();
            boolean canReserve = ReserveTimeJudge.judgeTime(timeStart,timeEnd);
            if (canReserve){
                int count = ManageReserve.insert(sno,(String)session.getAttribute("sname"),sclass,seatTno,tfloor,tsquare,timeStart,timeEnd);
                if(ManageReserve.doUpdateState(seatTno) == 0){
                    out.print("<script>");
                    out.print("alert('预约失败,原因：座位未及时更新！');window.location.href='reserveSelect' ");
                    out.print("</script>");
                }
                if (count>0){
                    out.print("<script>");
                    out.print("alert('预约成功');window.location.href='reserveSelect' ");
                    out.print("</script>");
                }else {
                    out.print("<script>");
                    out.print("alert('预约失败');window.location.href='reserveSelect' ");
                    out.print("</script>");
                }
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
}
